<?php
/**
 * Created by PhpStorm.
 * User: ysfl
 * Date: 2015/11/25
 * Time: 01:20
 * 向日葵模块
 */

namespace modules\admin;

use Cross\MVC\Module;

class InventoryModule extends Module
{

    /**
     * 库存表
     *
     * @var string
     */
    protected $tables = 'xrk_inventory';

    /**
     * 进货表
     *
     * @var string
     */
    protected $tablesi = 'xrk_inventory_i';

    /**
     * 调货记录表
     *
     * @var string
     */
    protected $I_Transfer_Log = 'xrk_inventory_transfer';

    /**
     * 添加商品
     *
     * @param $data
     * @return text
     */
    function addInventory($data)
    {
        //检查表单是否为空
        foreach ($data as $key => & $val) {

            if ($val == "") {

                switch($key)
                {
                    case 'i_name':
                        $name = "商品名称";
                        break;

                    case 'type':
                        $name = "商品类型";
                        break;

                    case 'c_price':
                        $name = "渠道价";
                        break;

                    case 'v_price':
                        $name = "会员价";
                        break;

                    case 'r_price':
                        $name = "零售价";
                        break;

                    case 's_name':
                        $name = "供应商";
                        break;

                    default:
                        $name = "未知错误";
                        break;
                }

                if ($data['img'] == NULL){}

                return '<div class="alert alert-danger" role="alert">请输入：<b style="color: #FFEA44;">'.$name.'</b></div>';

                unset($data[$key]);
            }

            $data['o_name'] = $_SESSION['uname'];
            $data['time'] = date('Y-m-d H:i:s');
        }

        //检测商品是否存在
        if ($this->link->get($this->tables, 'i_name', array('i_name' => $data['i_name']))){
            return '<div class="alert alert-danger" role="alert">这个商品库存已经存在！</div>';
        }

        //数据写入数据库
        if ($this->link->add($this->tables, $data)){
            return '<div class="alert alert-success" role="alert"><b style="color:red">'.$data['i_name'].'</b> 添加成功！</div>';
        }
        else{
            return '<div class="alert alert-danger" role="alert">添加失败！请联系管理员！</div>';
        }
    }

    /**
     * 查询商品
     * @return array
     */

    function queryinventory()
    {
        //查询并返回结果
        return $this->link->select('*')
            ->from('xrk_inventory')
            ->orderBy(array('type'))
            ->stmt()
            ->fetchAll(\PDO::FETCH_ASSOC);
    }

    /**
     * 查询店铺
     * @return array
     */

    function queryshops()
    {
        //查询并返回结果
        return $this->link->getAll("xrk_shops",'*');
    }

    /**
     * 获取商品分类
     * @return array
     */

    function Shops_Type()
    {

        //SQL语句单独写以备后面增加条件
        $s = $this->link->select('type')
                        ->from('xrk_inventory')
                        ->groupBy('type')
                        ->stmt()
                        ->fetchAll(\PDO::FETCH_ASSOC);

        //查询并返回结果
        return $s;
    }

    /**
     * AJAX二级联动菜单商品查询
     *
     * return array
     */

    function Q_Type($data)
    {
        //查询商品
        $s = $this->link->getALL('xrk_inventory','*',array('type' => $data['type']));

        //返回价格实例
        return $s;
    }

    /**
     * 进货登记
     *
     * @param $data
     * @return text
     */
    function addInventoryi($data)
    {
        //检查表单是否为空
        foreach ($data as $key => & $val) {

            if ($val == "") {

                switch($key)
                {
                    case 'i_name':
                        $name = "商品名称";
                        break;

                    case 'c_price':
                        $name = "进货单价";
                        break;

                    case 'i_num':
                        $name = "商品数量";
                        break;

                    case 'i_shops':
                        $name = "仓库名称";
                        break;

                    case 'c_name':
                        $name = "供应商";
                        break;

                    default:
                        $name = "未知错误";
                        break;
                }

                return '<div class="alert alert-danger" role="alert">请输入：<b style="color: #FFEA44;">'.$name.'</b></div>';

                unset($data[$key]);
            }
        }

        //筛选分店编码,方便以后添加分店
        switch($data['i_shops'])
        {
            case "总仓库":
                $addr = "warehouse";
                break;

            case "文化广场店":
                $addr = "wh";
                break;

            case "白露店":
                $addr = "bl";
                break;
        }

        //获取商品数据
        $item_info = $this->link->get($this->tables,'*',array('i_name' => $data['i_name']));

        $item_type = $item_info['type'] != "服务费" ? 0 : 1;

        //库存是否有货，服务费不计算库存数量
        if ($item_type){
            return "这个商品类型为服务费，不能添加库存数量";
        }

        //计算需要增加的库存数量
        $s = $item_info[$addr] + $data['i_num'];

        //执行扣除当前店面库存
        $inventory = $this->link->update($this->tables,array($addr => $s),array('i_name' => $data['i_name']));

        //组装进货数据
        $sql = $this->link->add($this->tablesi, array(

            //添加时间
            'time' => date('Y-m-d H:i:s'),

            //进货公司名字
            'c_name' => $data['c_name'],

            //商品名称
            'i_name' => $data['i_name'],

            //进货单价
            'c_price' => $data['c_price'],

            //数量
            'i_num' => $data['i_num'],

            //总价
            'i_sum' => $data['i_num'] * $data['c_price'],

            //仓库名字
            's_name' => $data['i_shops'],

            //进货人
            'name' => $_SESSION['uname']

        ));

        //数据写入数据库
        if ($sql && $inventory){
            return '<div class="alert alert-success addInventoryi" role="alert">
                成功进了
                <b>'.$data['i_num'].'</b>
                个
                <b>'.$data['i_name'].'</b>
                到
                <b>'.$data['i_shops'].'</b>
                ， 当前库存为：
                <b>'.$s.'</b>
                个</div>';
        }
        else{
            return '<div class="alert alert-success" role="alert">添加失败，请联系管理员</div>';
        }
    }

    /**
     * 内部仓库调货
     */
    function Inventory_Transfer($data)
    {

        //检查表单是否为空
        foreach ($data as $key => & $val) {

            if ($val == "") {

                switch($key)
                {
                    case 'i_name':
                        $name = "商品名称";
                        break;

                    case 'i_num':
                        $name = "商品数量";
                        break;

                    default:
                        $name = "未知错误";
                        break;
                }

                return '<div class="alert alert-danger" role="alert">请输入：<b style="color: #FFEA44;">'.$name.'</b></div>';

                unset($data[$key]);
            }
        }

        $Item_Name = $data['i_name'];
        $Item_Num = $data['i_num'];

        //筛选分店编码,方便以后添加分店
        switch($data['Source_Addr'])
        {
            case "总仓库":
                $Source_Addr = "warehouse";
                break;

            case "文化广场店":
                $Source_Addr = "wh";
                break;

            case "白露店":
                $Source_Addr = "bl";
                break;
        }

        //筛选分店编码,方便以后添加分店
        switch($data['Destinations'])
        {
            case "总仓库":
                $Destinations = "warehouse";
                break;

            case "文化广场店":
                $Destinations = "wh";
                break;

            case "白露店":
                $Destinations = "bl";
                break;
        }

        //检查源地址和目标地址是否一样
        if ($Source_Addr == $Destinations){
            return '<div class="alert alert-danger" role="alert">调货地址和目的地地址一样</div>';
        }

        //检查商品数量是否小于或者为0,避免错误的提交
        if ($Item_Num <= 0){
            return '<div class="alert alert-danger" role="alert">商品数量的值不在正常范围内，请填写正确的数</div>';
        }

        //获取商品数据
        $Source_info = $this->link->get($this->tables,array('id',$Source_Addr,'type'),array('i_name' => $Item_Name));

        //没有库存提示，避免扣除库存后的二次重复提交
        if ($Source_info[$Source_Addr] == 0){

            return '<div class="alert alert-danger" role="alert">这个商品的库存数量为0</div>';

        }

        //检查要扣除的数量是否超过源库存
        if ($Item_Num > $Source_info[$Source_Addr]){

            return '<div class="alert alert-danger" role="alert">这个商品没有这么多库存</div>';

        }

        //获取商品数据
        $Destinations_info = $this->link->get($this->tables,array($Destinations,'type'),array('i_name' => $Item_Name));

        //计算需要扣除的源库存数量
        $Source_Num = $Source_info[$Source_Addr] - $Item_Num;

        //计算要增加的目标库存数量
        $Destinations_Num = $Destinations_info[$Destinations] + $Item_Num;

        //源库存扣除语句
        $Source = $this->link->update($this->tables,array($Source_Addr => $Source_Num),array('i_name' => $Item_Name));

        //目标库存增加语句
        $Destinations = $this->link->update($this->tables,array($Destinations => $Destinations_Num),array('i_name' => $Item_Name));

        //操作信息记录
        $Transfer_Log = $this->link->add($this->I_Transfer_Log, array(

            //操作时间
            'time' => date('Y-m-d H:i:s'),

            //进货公司名字
            'name' => $_SESSION['uname'],

            //商品名称
            'item' => $Item_Name,

            //商品编号
            'i_id' => $Source_info['id'],

            //源库存地址
            'source_addr' => $data['Source_Addr'],

            //目标库存地址
            'destinations' => $data['Destinations'],

            //数量
            'num' => $Item_Num

        ));

        //执行修改
        if ($Source && $Destinations && $Transfer_Log){

            //返回信息
            return '<div class="alert alert-success Inventory_Transfer_return" role="alert">
                    调货成功！ 从
                    <b>'.$data['Source_Addr'].'</b>
                    调取了
                    <b>'.$Item_Num.'</b>
                    个
                    <b>'.$Item_Name.'</b>
                    到
                    <b>'.$data['Destinations'].'</b>
                    当前
                    <b>'.$data['Destinations'].'</b>
                    库存为
                    <b>'.$Destinations_Num.'</b>
                    个</div>';

        }
    }

    //获取进货、调货操作记录
    function Inventory_Log($data)
    {
        return $this->link->select($data['select'])
            ->from($data['table'])
            ->orderBy($data['order'])
            ->limit($data['limit'])
            ->stmt()
            ->fetchAll(\PDO::FETCH_ASSOC);
    }
    
    //获取未付款记录
    function Not_Payment()
    {
        return $this->link->select('sum(i_sum) as price,c_name as name')
            ->from($this->tablesi)
            ->where('start = 0')
            ->groupBy('c_name')
            ->stmt()
            ->fetchAll(\PDO::FETCH_ASSOC);
    }
}